const Mongoose = require("./db");
const house = require("../counts/house.js");
const formatDateHelper = require('../utils/formatDateHelper.js')

const houseSchema = new Mongoose.Schema({
	id: {
		type: Number,
		unique: true,
	},
	name: String,
	create: {
		type: String,
		default: formatDateHelper.date() 
	},
	modify: {
		type: String,
		default: formatDateHelper.date()
	}
})


houseSchema.pre("save", async function(next) {
	const self = this;
	try {
		if (!self.id) {
			self.id = await house.getNextSequenceValue("id");
		}
	} catch (err) {
		next(err);
	}
});

const houseModel = Mongoose.model("houses", houseSchema);

module.exports = houseModel;